from rest_framework import serializers
from ..models.BOM import *


# BOM单
class FaKuyouBomSerializer(serializers.ModelSerializer):
    total_cost = serializers.SerializerMethodField()
    status = serializers.SerializerMethodField()

    class Meta:
        model = FaKuyouBom
        fields = '__all__'

    def get_total_cost(self, obj):
        return obj.total_cost

    def get_status(self, obj):
        """示例：根据时间判断状态（可根据业务需求调整）"""
        if not obj.updatetime:
            return 'draft'
        return 'active'

    def validate(self, data):
        if data.get('number', 0) < 0:
            raise serializers.ValidationError("数量不能为负数")
        if data.get('wages', 0) < 0:
            raise serializers.ValidationError("计件工资不能为负数")
        return data


# BOM商品单
class FaKuyouBomInfoSerializer(serializers.ModelSerializer):

    class Meta:
        model = FaKuyouBomInfo
        fields = '__all__'

    def validate(self, data):
        """验证数据"""
        if data.get('number', 0) < 0:
            raise serializers.ValidationError("数量不能为负数")
        if data.get('purchase', 0) < 0:
            raise serializers.ValidationError("进价不能为负数")
        return data


# BOM工序单
class FaKuyouBomProcedureSerializer(serializers.ModelSerializer):
    total_wages = serializers.SerializerMethodField(help_text='总工资')

    class Meta:
        model = FaKuyouBomProcedure
        fields = '__all__'

    def get_total_wages(self, obj):
        return obj.total_wages

    def validate(self, data):
        """验证数据"""
        if data.get('wages', 0) < 0:
            raise serializers.ValidationError("单位工资不能为负数")
        if data.get('hours', 0) < 0:
            raise serializers.ValidationError("标准工时不能为负数")
        return data
